﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TMDT_Nhom1A.BUS;
using TMDT_Nhom1A.DTO;
using System.Drawing;

namespace TMDT_Nhom1A.UserControl_Admin
{
    public partial class AccountManager : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                gvAccountManager.DataSource = Customer_BUS.getAllAccount();
                gvAccountManager.DataBind();
            }
        }

        protected void gvAccountManager_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbStatus = (Label)e.Row.FindControl("lbStatus");
                Label lbActivate = (Label)e.Row.FindControl("lbActivate");
                System.Web.UI.WebControls.Image imgUserStatus = (System.Web.UI.WebControls.Image)e.Row.FindControl("ImgUserStatus");
                System.Web.UI.WebControls.Image imgActivateStatus = (System.Web.UI.WebControls.Image)e.Row.FindControl("ImgActivateStatus");

                if (lbStatus.Text == "0")
                {
                    imgUserStatus.ImageUrl = "~/images/Status-user-busy-icon.png";
                    imgUserStatus.ToolTip = "bị khoá";
                }
                else
                {
                    imgUserStatus.ImageUrl = "~/images/Status-user-online-icon.png";
                    imgUserStatus.ToolTip = "đang sử dụng";
                }

                if (lbActivate.Text == "True")
                {
                    imgActivateStatus.ImageUrl = "~/images/activate-icon.png";
                    imgActivateStatus.ToolTip = "đã kích hoạt";
                }
                else
                {
                    imgActivateStatus.ImageUrl = "~/images/not-activate-icon.gif";
                    imgActivateStatus.ToolTip = "chưa kích hoạt";
                }
            }
        }

        protected void gvAccountManager_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAccountManager.PageIndex = e.NewPageIndex;

            //lien ket du lieu
            gvAccountManager.DataSource = Customer_BUS.getAllAccount();
            gvAccountManager.DataBind();

            
        }

        protected void gvAccountManager_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ViewDetail")
            {
                int userID = int.Parse(e.CommandArgument.ToString());

                //lay ra thong tin do vao detail view
                DtlUserInfo.EditItemIndex = -1;
                DtlUserInfo.DataSource = Customer_BUS.getCustomerInfoByUserID(userID);
                DtlUserInfo.DataBind();

                
            }
            if (e.CommandName == "EditDetail")
            {
                int userID = int.Parse(e.CommandArgument.ToString());

                DtlUserInfo.EditItemIndex = 0;
                //lay ra thong tin do vao detail view
                DtlUserInfo.DataSource = Customer_BUS.getCustomerInfoByUserID(userID);
                DtlUserInfo.DataBind();


            }
        }

        protected void DtlUserInfo_ItemCommand(object source, DataListCommandEventArgs e)
        {
            
            if (e.CommandName == "Update")
            {
                DropDownList drlStatus = (DropDownList)e.Item.FindControl("drlStatus");
                DropDownList drlActivate = (DropDownList)e.Item.FindControl("drlActivate");

                TextBox tbAddressCompany = (TextBox)e.Item.FindControl("tbAddressCompany");
                TextBox tbEmail = (TextBox)e.Item.FindControl("tbEmail");
                TextBox tbTaxCode = (TextBox)e.Item.FindControl("tbTaxCode");
                TextBox tbNamePresentation = (TextBox)e.Item.FindControl("tbNamePresentation");
                TextBox tbRole = (TextBox)e.Item.FindControl("tbRole");
                TextBox tbNameCompany = (TextBox)e.Item.FindControl("tbNameCompany");
                TextBox tbPhone = (TextBox)e.Item.FindControl("tbPhone");
                TextBox tbMobile = (TextBox)e.Item.FindControl("tbMobile");
                TextBox tbDateRegistry = (TextBox)e.Item.FindControl("tbDateRegistry");
                

                //thuc hien cap nhat
                int userID = int.Parse(e.CommandArgument.ToString());
                Customer_DTO customer = new Customer_DTO();
                customer.AddressCompany = tbAddressCompany.Text;
                customer.Email = tbEmail.Text;
                customer.TaxCode = tbTaxCode.Text;
                customer.NamePresentation = tbNamePresentation.Text;
                customer.Role = tbRole.Text;
                customer.NameCompany = tbNameCompany.Text;
                customer.Phone = tbPhone.Text;
                customer.Mobile = tbMobile.Text;
                customer.DateRegistry = DateTime.Parse(tbDateRegistry.Text);
                customer.Status = int.Parse(drlStatus.SelectedValue);
                customer.Activate = bool.Parse(drlActivate.SelectedValue);
                customer.UserID = userID;

                //luu xuong database
                bool result = Customer_BUS.updateUserInfo(customer);
                if (result == true)
                {
                    
                    LbThongBao.Text = "Thành Công";
                    LbThongBao.ForeColor = Color.Blue;

                    //refesh gridview
                    gvAccountManager.DataSource = Customer_BUS.getAllAccount();
                    gvAccountManager.DataBind();
                }
                else
                {
                    LbThongBao.Text = "Thất Bại";
                    LbThongBao.ForeColor = Color.Red;
                }

            }

            if (e.CommandName == "Cancel")
            {
                int userID = int.Parse(e.CommandArgument.ToString());

                //lay ra thong tin do vao detail view
                DtlUserInfo.EditItemIndex = -1;
                DtlUserInfo.DataSource = Customer_BUS.getCustomerInfoByUserID(userID);
                DtlUserInfo.DataBind();
            }
        }

        protected void DtlUserInfo_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Label lbStatus = (Label)e.Item.FindControl("lbStatus");
            Label lbActivate = (Label)e.Item.FindControl("lbActivate");
            DropDownList drlStatus = (DropDownList)e.Item.FindControl("drlStatus");
            DropDownList drlActivate = (DropDownList)e.Item.FindControl("drlActivate");

            drlStatus.Items.Add(new ListItem("Khoá", "0"));
            drlStatus.Items.Add(new ListItem("Cho Phép Sử Dụng", "1"));

            drlActivate.Items.Add(new ListItem("Kích Hoạt", "True"));
            drlActivate.Items.Add(new ListItem("Không Kích Hoạt", "False"));
          
            if (lbStatus.Text == "0")
            {
                drlStatus.SelectedIndex = 0;
            }
            else
            {
                drlStatus.SelectedIndex = 1;
            }

            if (lbActivate.Text == "True")
            {
                drlActivate.SelectedIndex = 0;
            }
            else
            {
                drlActivate.SelectedIndex = 1;
            }
        }

        protected void btTimKiem_Click(object sender, EventArgs e)
        {
            //tim theo username
            gvAccountManager.DataSource = Customer_BUS.searchUserByUsername(tbUsername.Text);
            gvAccountManager.DataBind();
        }

       


    }
}